<?php

/**
 * This is the model class for table "confession".
 *
 * The followings are the available columns in table 'confession':
 * @property string $id
 * @property string $content
 * @property string $name
 * @property integer $year
 * @property string $postdate
 */
class Confession extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Confession the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'confession';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('content, year', 'required'),
            array('year', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 20),
            array('content, postdate', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, content, name, year, postdate', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'content' => 'Content',
            'name' => 'Name',
            'year' => 'Year',
            'postdate' => 'Postdate',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('year', $this->year);
        $criteria->compare('postdate', $this->postdate, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * set timestamp before save
     */
    public function beforeSave() {
        if ($this->isNewRecord)
            $this->postdate = new CDbExpression('NOW()');
        return parent::beforeSave();
    }

    public function getConfessions($startpoint) {
        $criteria = new CDbCriteria(array(
            'select' => '*',
            // 'condition'=>'status=' . Post::STATUS_PUBLISHED,
            'limit' => Constant::NUMBER_NEW_PER_PAGE,
            'offset' => $startpoint,
            'order' => 'postdate DESC'
        ));
        $confessions = Confession::model()->findAll($criteria);
        return $confessions;
    }

    public function listConfessions() {
        $criteria = new CDbCriteria(array(
            'select' => '*',
            'order' => 'postdate DESC'
        ));

        // count result to display multipages
        $count = Confession::model()->count($criteria);
        $pages = new CPagination($count);

        $pages->pageSize = Constant::NUMBER_NEW_PER_PAGE;

        $pages->applyLimit($criteria);

        $confessions = Confession::model()->findAll($criteria);

        return array('confessions' => $confessions, 'pages' => $pages);
    }

}